<template>
  <div class="form-box">
    <van-form @submit="onSubmit">
      <van-field v-model="username" name="手机号码" label="手机号码:" placeholder="请填写手机号码" :rules="[{ required: true, message: '请填写用户名' }]" />
      <van-field v-model="password" type="password" name="密码" label="密码:" placeholder="请填写密码" :rules="[{ required: true, message: '请填写密码' }]" />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit" color="#ff3a3a">点击登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script  type='module'>
export default {
  name: 'LoginForm',
  data () {
    return {
      // 登录手机号
      username: '13378681755',
      // 登录密码
      password: 'ahj28685',
      // 登录成功返回的用户信息
      userInfo: []
    }
  },
  methods: {
    // 点击确认登录
    async onSubmit (values) {
      const res = await this.$http({ url: '/login/cellphone', params: { phone: this.username, password: this.password } })
      if (res.code === 200) {
        this.$notify({ type: 'success', message: '登录成功！！' })
        const userInfo = JSON.stringify(res.profile)
        sessionStorage.setItem('userInfo', userInfo)
        sessionStorage.setItem('token', res.token)
        // 登录成功后刷新页面
        window.location.reload()
        // 登录成功后回到首页
        // this.$router.push('/')
      } else if (res.code === 400) {
        return this.$notify({ type: 'danger ', message: '手机号错误！' })
      } else if (res.code === 502) {
        return this.$notify({ type: 'danger ', message: '密码错误！' })
      } else {
        return this.$notify({ type: 'warning', message: '登录失败！！' })
      }
    }
  }
}
</script>

<style scoped lang="less">
 .form-box {
    width: 80%;
    position: absolute;
    bottom: 200px;
    left: 50%;
    transform: translateX(-50%);
  }
</style>
